#include "logonwidget.h"
#include "user.h"
#include "ui_logonwidget.h"
#include "maininterfacewidget.h"
#include <QHostAddress>
#include "registerwidget.h"
#include <cstring>


LogOnWidget::LogOnWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LogOnWidget)
{
    ui->setupUi(this);
    //socket = new QTcpSocket;
    this->setWindowTitle("登录");
    QIcon icon(":/asset/image/logoicon.jpg");
    this->setWindowIcon(icon);
    ui->passwordLineEdit->setEchoMode(QLineEdit::Password);
}

LogOnWidget::~LogOnWidget()
{
    delete ui;
}

void LogOnWidget::on_btn_login_clicked()//登录
{
    //get username and password
    QString username = ui->usernameLineEdit->text();
    QString password = ui->passwordLineEdit->text();
    //connect to server
    QNetworkRequest request(QUrl("http://101.43.168.188:19999/slimarveous/login"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    QJsonObject json;
    json.insert("username", username);
    json.insert("password", password);
    //qDebug()<<json;
    QJsonDocument doc(json);
    QByteArray data = doc.toJson(QJsonDocument::Compact);
    QNetworkReply *reply = manager.post(request, data);
    connect(reply, &QNetworkReply::finished, [=]()
    {
        if (reply->error() == QNetworkReply::NoError)
        {
            // 读取响应数据
            QByteArray dataReply = reply->readAll();
            qDebug()<<dataReply;
            // 将 JSON 数据转换为 QVariant 对象
            QJsonParseError error;
            QJsonObject result = QJsonDocument::fromJson(dataReply, &error).object();
            // 处理 JSON 数据
            if (error.error == QJsonParseError::NoError)
            {
                QVariantMap resultMap = result.toVariantMap();
                //提取状态码
                QVariant status_code = resultMap.value("status_code");
                status_code.toString();
                //提取user对应的Map
                if(resultMap.contains("user"))
                {
                    QMap userMap = resultMap.value("user").toMap();
                    QString id = userMap.value("id").toString();
                    user userSelf;
                    userSelf.setUserName(username);
                    userSelf.setUserId(id);
                    userSelf.setUserPassword(password);
                    //开始判断登录是否有效
                    if(status_code == "2")
                    {
                        QMessageBox::information(this, "登录成功", "你好，"+username+"! "+"your id is " +id);
                        this->close();
                        //跳转到friendlist页面
                        friendlist* list = new friendlist(nullptr, userSelf);
                        list->show();
                    }//登录成功
                    else//登录失败
                        QMessageBox::information(this, "登录失败", "用户名或密码有误！");
                }
            }
        }
        else
            qDebug()<<"error";
        // 释放资源
        reply->deleteLater();
    });
}

void LogOnWidget::on_btn_signup_clicked()//注册
{
    this->close();
    RegisterWidget* registerWidget = new RegisterWidget();
    registerWidget->show();
}

